package com.cs.user.application.controller;

import com.cs.user.application.dtos.RoleDTO;
import com.cs.user.common.entity.BaseResponse;
import com.cs.user.common.utlis.ResultUtils;
import com.cs.user.domain.bos.RoleBO;
import com.cs.user.domain.service.RoleDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleDomainService roleDomainService;

    /**
     * 添加角色
     */
    @PostMapping("/add")
    public BaseResponse<Boolean> addRole(@RequestBody RoleDTO roleDTO){
        // 输出日志
        log.info("添加角色请求参数：{}", roleDTO);
        // 类型转换
        RoleBO roleBO = new RoleBO();
        BeanUtils.copyProperties(roleDTO, roleBO);
        // 调用domain层方法
        return ResultUtils.success(roleDomainService.addRole(roleBO));
    }

    /**
     * 删除角色
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteRole(@RequestBody RoleDTO roleDTO){
        // 输出日志
        log.info("删除角色请求参数：{}", roleDTO);
        // 类型转换
        RoleBO roleBO = new RoleBO();
        BeanUtils.copyProperties(roleDTO, roleBO);
        // 调用domain层方法
        return ResultUtils.success(roleDomainService.deleteRole(roleBO));
    }

    /**
     * 更新角色
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateRole(@RequestBody RoleDTO roleDTO){
        // 输出日志
        log.info("更新角色请求参数：{}", roleDTO);
        // 类型转换
        RoleBO roleBO = new RoleBO();
        BeanUtils.copyProperties(roleDTO, roleBO);
        // 调用domain层方法
        return ResultUtils.success(roleDomainService.updateRole(roleBO));
    }
}
